package com.meitianhui.convenitenbuy.module.order.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding.view.RxView;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.ui.activity.web.AdWebViewActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.OrderInfo;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import rx.functions.Action1;

/**
 * Created by Administrator on 2017/11/16.
 */

public class FightGroupOrderAdapter extends BaseQuickAdapter<OrderInfo.ListBean, BaseViewHolder> {
    private Context mContext;
    private Map<String, String> mUrlMap;
    private OnClickOrder mOnclickOrder;
    public FightGroupOrderAdapter(Context context, @Nullable List<OrderInfo.ListBean> data, Map<String, String> urlMap, OnClickOrder onclickOrder) {
        super(R.layout.item_fight_group_order, data);
        mContext = context;
        mUrlMap = urlMap;
        mOnclickOrder = onclickOrder;
    }


    @Override
    protected void convert(BaseViewHolder helper, OrderInfo.ListBean item) {
        Button btnDetail = helper.getView(R.id.btn_detail);
        Button btnLogistic = helper.getView(R.id.btn_logistic);
        Button btnCancel = helper.getView(R.id.btn_cancel);
        Button btnConfirm = helper.getView(R.id.btn_confirm);
        helper.setText(R.id.tv_order_number, "订单编号 " + item.getOrder_no());   //订单号
        helper.setText(R.id.tv_good_name, item.getDesc1());   //介绍
        helper.setText(R.id.tv_status, item.orderStatus());   //订单状态
        helper.setText(R.id.tv_code, item.getLoaded_code());   //提货码
        helper.setText(R.id.tv_create_date, item.getOrder_date());   //创建时间
        helper.setText(R.id.tv_address, item.getAddress());   //收货地址
        helper.setText(R.id.tv_fight_price, "¥" + item.getDeposit_fee());   //成团价格
        helper.setText(R.id.tv_received_mode, item.getReceivedMode());   //收货方式
        String picInfo = item.getGoods_json_data();
        RxView.clicks(btnDetail).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (mOnclickOrder != null) {
                    mOnclickOrder.detailClick(item.getOrder_id(),item.getActivity_id());
                }
            }
        });
        RxView.clicks(btnConfirm).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (mOnclickOrder != null) {
                    mOnclickOrder.confirmClick(item.getOrder_id());
                }
            }
        });
        RxView.clicks(btnCancel).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (mOnclickOrder != null) {
                    mOnclickOrder.cancelClick(item.getOrder_id());
                }
            }
        });
        if (!TextUtils.isEmpty(picInfo)) {
            try {
                JSONObject jsonObject = new JSONObject(picInfo);
                String img = jsonObject.getString("path_id");
                img = mUrlMap.get(img);
                GlideUtils.setCircleImage(mContext,img, R.mipmap.wallet_coupon_item_default, R.mipmap.wallet_coupon_item_default, helper.getView(R.id.iv_good));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if("paid".equals(item.getStatus())){//已支付
            btnCancel.setVisibility(View.VISIBLE);
            btnConfirm.setVisibility(View.GONE);
            btnLogistic.setVisibility(View.GONE);
        }else if("delivered".equals(item.getStatus())){//已发货
            btnCancel.setVisibility(View.GONE);
            btnConfirm.setVisibility(View.VISIBLE);
            btnLogistic.setVisibility(View.GONE);
        }else{
            btnCancel.setVisibility(View.GONE);
            btnConfirm.setVisibility(View.GONE);
            btnLogistic.setVisibility(View.GONE);
        }

        if("delivered".equals(item.getStatus())||"received".equals(item.getStatus())||"refunded".equals(item.getStatus())) {//已发货 或者已收货
            String s_log = item.getLogistics();
            if (!TextUtils.isEmpty(s_log)) {
                try {
                    JSONObject jsonObject = new JSONObject(s_log);
                    String s_company = jsonObject.getString("company");
                    final String s_number = jsonObject.getString("number");
                    if (!TextUtils.isEmpty(s_company) && !TextUtils.isEmpty(s_number)) {
                        btnLogistic.setVisibility(View.VISIBLE);
                        btnLogistic.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, AdWebViewActivity.class);
                                intent.putExtra("url", Constants.HOST + "h5/c/hpt/logistics"
                                        + "?logistics_number=" + s_number);
                                intent.putExtra("title", "查看物流");
                                mContext.startActivity(intent);
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void setNewData(List data, Map<String, String> map) {
        setNewData(data);
        this.mUrlMap = map;
    }

    public void addData(List data, Map<String, String> map) {
        addData(data);
        mUrlMap.putAll(map);
    }

    public interface OnClickOrder{
        void cancelClick(String orderId);
        void confirmClick(String orderId);
        void detailClick(String orderId,String activityId);
    }

}
